#include "faceboxui.h"
#include <QtWidgets\qfiledialog.h>
#include <qdebug.h>
#include <cmath>
#include <sstream>
//using namespace arma;

FaceboxUI::FaceboxUI(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	dir += "/";
	show_ = new QFigure();
	fileName = QFileDialog::getOpenFileName(this, tr("open file"), "", tr("Data file (*.txt);;Allfile(*.*)"));
	loadTXTFile(fileName, data, files);
	QImage img;
	img.load(dir+files[0]);
	show_->setPixmap(QPixmap::fromImage(img));
	show_->SetROI(data[0]);
	//show_->resize(QSize(img.width(), img.height()));
	ui.drawArea_2->setWidget(show_);
	ui.drawArea_2->setAlignment(Qt::AlignTop);
	id1 = 0;
	id2 = 0;
	ui.picnum->setText(QString::number(id1 + 1, 10));
	ui.picnum_2->setText(QString::number(id2 + 1, 10));	
}

void loadTXTFile(QString filename, QList<QList<bBox>> & data, QList<QString> &files) 
{
	std::ifstream f(filename.toStdString());
	QString temp="";
	std::string t;
	QString t1;
	int idx, x, y, w, h;
	while (f>>t&&f>>idx&&f>>x&&f>>y&&f>>w&&f>>h) 
	{
		t1=QString::fromStdString(t);
		if (t1 == temp)
		{
			bBox b;
			b.idx = idx;
			b.box.setTopLeft(QPoint(x, y));
			b.box.setWidth(w);
			b.box.setHeight(h);
			data[data.size()-1].push_back(b);
		}
		else 
		{
			QList<bBox> qb;
			bBox b;
			b.idx = idx;
			b.box.setTopLeft(QPoint(x, y));
			b.box.setWidth(w);
			b.box.setHeight(h);
			qb.push_back(b);
			data.push_back(qb);
			files.push_back(t1);
		}
		temp = t1;
	}

}

FaceboxUI::~FaceboxUI()
{

}

void getFiles(std::string path, QList<QString>& files)
{
	//wen jian ju bing 
	intptr_t  hFile = 0;
	//wen jian xin xi 
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			//ru guo shi mu lu ze die dai
			//ru guo shi wen jian jia ru
			if ((fileinfo.attrib &  _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(QString::fromStdString(p.assign(path).append("\\").append(fileinfo.name)));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

void saveData(QString filename, QString file, QList<bBox>&data)
{
	std::ofstream f(filename.toStdString(), std::ios::app);

	for (int i = 0; i < data.size(); i++)
	{
		f << file.toStdString() << " " << data[i].idx << " " << data[i].box.left() << " " << data[i].box.top() << " " << data[i].box.width() << " " << data[i].box.height() << std::endl;
	}
	f.close();
	return;
}

void FaceboxUI::on_pRect_clicked()
{
	show_->setBrushType(QFigure::pRect);
}

void FaceboxUI::on_mRect_clicked()
{
	show_->setBrushType(QFigure::mRect);
}

void FaceboxUI::on_undo_clicked()
{
	show_->clear();
}

void FaceboxUI::on_picleft_clicked()
{
	saveData(dir + "rlt.txt", files[id1], data[id1]);
	id2 = 0;
	id1 = (files.size() + --id1) % files.size();
	QImage img;
	img.load(dir + files[id1]);
	ui.picnum->setText(QString::number(id1 + 1, 10));
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
	show_->setPixmap(QPixmap::fromImage(img));
	show_->SetROI(data[id1]);

	ui.drawArea_2->setWidget(show_);
}

void FaceboxUI::on_picright_clicked()
{
	saveData(dir + "rlt.txt", files[id1], data[id1]);
	id2 = 0;
	id1 = ++id1%files.size();
	QImage img;
	img.load(dir + files[id1]);
	ui.picnum->setText(QString::number(id1 + 1, 10));
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
	show_->setPixmap(QPixmap::fromImage(img));
	show_->SetROI(data[id1]);
	//show_->resize(QSize(img.width(), img.height()));
	ui.drawArea_2->setWidget(show_);
}

void FaceboxUI::on_boxleft_clicked()
{
	int num = data[id1].size();
	id2 = (num + --id2) % num;
	show_->choseBox(id2);
	show_->draw();
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
}

void FaceboxUI::on_boxright_clicked()
{
	int num = data[id1].size();
	id2 = (num + ++id2) % num;
	show_->choseBox(id2);
	show_->draw();
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
}

void FaceboxUI::on_pushButton_clicked()
{
	saveData(dir + "rlt.txt", files[id1], data[id1]);
	id2 = 0;
	id1 = ui.picnum->toPlainText().toInt()-1;
	QImage img;
	img.load(dir + files[id1]);
	ui.picnum->setText(QString::number(id1 + 1, 10));
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
	show_->setPixmap(QPixmap::fromImage(img));
	show_->SetROI(data[id1]);
	//show_->resize(QSize(img.width(), img.height()));
	ui.drawArea_2->setWidget(show_);
}

void FaceboxUI::on_pushButton_2_clicked()
{
	int num = data[id1].size();
	id2 = ui.picnum_2->toPlainText().toInt() - 1;
	show_->choseBox(id2);
	show_->draw();
	ui.picnum_2->setText(QString::number(id2 + 1, 10));
}
